/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui.util;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JPopupMenu;

import org.bushe.swing.event.EventBus;

import who.tmap.bo.ThematicMapLayer;
import who.tmap.ui.event.MultiLayerCheckBoxListClicked;

public class MultiLayerThematicMapPopupMenu extends JPopupMenu {
	
	private ThematicMapLayer layer;
	private JCheckBoxMenuItem jmiBaseMap;
	private JCheckBoxMenuItem jmiCD;
	private JCheckBoxMenuItem jmiAnnotations;
	//private Observable2 observable =new Observable2();
	
	public MultiLayerThematicMapPopupMenu() {
		super();
		
		init();
	}
	
	private void init() {
		getBaseMapCB();
	    
	    getCDCB();
	    
	    getAnnotationsCB();
	}
	
	private void initValues() {
	    jmiBaseMap.setSelected( layer.isBaseMapVisible() );
	    jmiCD.setSelected( layer.isCdVisible() );
	    jmiAnnotations.setSelected( layer.isAnnotationsVisible() );
	}
	
	public void setLayer( ThematicMapLayer layer ) {
		this.layer =layer;
		
		initValues();
	}
	
	public JCheckBoxMenuItem getBaseMapCB() {
		if ( jmiBaseMap == null ) {
			jmiBaseMap = new JCheckBoxMenuItem("Base Map");
			
			jmiBaseMap.addActionListener( new ActionListener() {
			      public void actionPerformed(ActionEvent event) {
			    	  checkBoxClicked( event, jmiBaseMap );
			      }
			} );
			
		    add( jmiBaseMap );
		    
		}
		
		return jmiBaseMap;
	}
	
	public JCheckBoxMenuItem getCDCB() {
		if ( jmiCD == null ) {
			jmiCD = new JCheckBoxMenuItem("Cartographic Display");
			
			jmiCD.addActionListener( new ActionListener() {
			      public void actionPerformed(ActionEvent event) {
			    	  checkBoxClicked( event, jmiCD );
			      }
			} );
			
		    add( jmiCD );
		    
		}
		
		return jmiCD;
	}	
	
	public JCheckBoxMenuItem getAnnotationsCB() {
		if ( jmiAnnotations == null ) {
			jmiAnnotations = new JCheckBoxMenuItem("Annotations");
			
			jmiAnnotations.addActionListener( new ActionListener() {
			      public void actionPerformed(ActionEvent event) {
			    	  checkBoxClicked( event, jmiAnnotations );
			      }
			} );
			
		    add( jmiAnnotations );
		    
		}
		
		return jmiAnnotations;
	}	
	
	private void checkBoxClicked( ActionEvent event, JCheckBoxMenuItem checkBox ) {
        AbstractButton aButton = (AbstractButton) event.getSource();
        boolean selected = aButton.getModel().isSelected();
        
        if ( checkBox == jmiBaseMap ) {
        	layer.setBaseMapVisible( selected );
        } else if ( checkBox == jmiCD ) {
        	layer.setCdVisible( selected );
        } else if ( checkBox == jmiAnnotations ) {
        	layer.setAnnotationsVisible( selected );
        }
        
        /*observable.setChanged();
        observable.notifyObservers();*/
        EventBus.publish( new MultiLayerCheckBoxListClicked() );
	}
	
	/*public void addObserver(Observer o) {
		observable.addObserver(o);
	}*/
	
}
